﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shop.Intermediate;
using Shop.Intermediate.Data;
using Shop.Intermediate.Repository;
using DoNet.Common.Utilities;

namespace Shop.Data.LinqToEntityProvider
{
    public class DepartmentRepository:IDepartmentRepository,IPageSplit
    {
        private ShopObjectContext m_context;

        public DepartmentRepository(ShopObjectContext context)
        {
            this.m_context = context;
        }
    
        public virtual void Create(IDepartment department)
        {
            m_context.AddToDepartment(department as Department);
        }

        public virtual void Update(IDepartment department)
        {
            Department o1 = m_context.Department.FirstOrDefault(p => p.Code == department.Code);
            m_context.ApplyPropertyChanges("Department", department);
        }

        public virtual IDepartment GetEntity(string code)
        {
            //Department dpt = context.Department.FirstOrDefault(p => p.Code == code);
            //return dpt;
            object o = null;
            m_context.TryGetObjectByKey(new System.Data.EntityKey("ShopObjectContext.Department", "Code", code), out o);
            return o as IDepartment;
        }

        public virtual void Delete(string code)
        {
            //Department dpt = context.Department.FirstOrDefault(p => p.Code == code);
            IDepartment dpt = new Department{ Code = code };
            m_context.AttachTo("Department", dpt);
            m_context.DeleteObject(dpt);
        }

        public virtual IEnumerable<IDepartment> GetList()
        {
            IEnumerable<Department> query= from p in m_context.Department
                                           select p;
            return query.Cast<IDepartment>();
        }

        public virtual IEnumerable<IDepartment> GetList(string condition, object[] obj)
        {
            if (string.IsNullOrEmpty(condition) || obj.Length < 1)
            {
                return GetList();
            }

            IEnumerable<Department> query = (
                                             from p in m_context.Department
                                             select p
                                             )
                                             .Where(condition, obj);

            return query.Cast<IDepartment>();
        }

        public virtual IEnumerable<IDepartment> GetList(string condition, object[] whereObj, string ordering, object[] orderObj)
        {

            if (string.IsNullOrEmpty(ordering) || orderObj.Length < 1)
            {
                return GetList(condition, whereObj);
            }

            IEnumerable<Department> query;

            if (string.IsNullOrEmpty(condition) || ordering.Length < 1)
            {
                query = (
                        from p in m_context.Department
                        select p
                        )
                        .OrderBy(ordering, orderObj);
            }
            else
            {
                query = (
                         from p in m_context.Department
                         select p
                         )
                         .Where(condition, whereObj)
                         .OrderBy(ordering, orderObj);
            }

            return query.Cast<IDepartment>();
        }

        public virtual IEnumerable<IDepartment> PageResultQuery<IDepartment>(string condition, object[] whereObj, string ordering, object[] orderObj, int pageNo, int pageSize, out int pageCount, out int recordCount)
        {
            IEnumerable<Department> query;
            if (string.IsNullOrEmpty(ordering) || orderObj.Length < 1)
            {
                ordering = "Code";
                orderObj = new object[] { "ascending" };
            }

            if (string.IsNullOrEmpty(condition) || whereObj.Length < 1)
            {
                recordCount = (
                                 from p in m_context.Department
                                 select p
                                )
                                .Count();

                pageCount = recordCount / pageSize;

                if (recordCount % pageSize > 0)
                {
                    pageCount = pageCount + 1;
                }

                int startNum = (pageNo - 1) * pageSize;

                query = (
                        from p in m_context.Department
                        select p
                        )
                        .OrderBy(ordering, orderObj)
                        .Skip(startNum)
                        .Take(pageSize);
            }
            else
            {
                recordCount = (
                               from p in m_context.Department
                               select p
                              )
                              .Where(condition, whereObj)
                              .Count();

                pageCount = recordCount / pageSize;

                if (recordCount % pageSize > 0)
                {
                    pageCount = pageCount + 1;
                }

                int startNum = (pageNo - 1) * pageSize;

                query = (
                         from p in m_context.Department
                         select p
                         )
                         .Where(condition, whereObj)
                         .OrderBy(ordering, orderObj)
                         .Skip(startNum)
                         .Take(pageSize);
            }
            return query.Cast<IDepartment>();
        }

        public void SubmitChanges()
        {
            m_context.SaveChanges();
        }

    }
}
